import Vue from 'vue'
import App from './App'
import {GYZJMODEL as gyzjModel} from '@/utils/gyzjModel.js'

import pageHead from './components/page-head.vue'
import pageFoot from './components/page-foot.vue'
import uLink from '@/components/uLink.vue'
import store from './store'
//main.js里面配置全局组件
// import uniRate from './components/uni-rate/uni-rate.vue'

// 添加统计
// const Stat = require('./statistics/dcloud-stat');
// Stat.init(Vue);

Vue.config.productionTip = false

Vue.prototype.$store = store
Vue.prototype.$backgroundAudioData = {
	playing: false,
	playTime: 0,
	formatedPlayTime: '00:00:00'
}
/*app的登录*/
Vue.prototype.main_login=function(code){
	console.log('执行了登陆进来',code);
	// 1.wx获取登录用户code
	return new Promise((resolve,reject)=>{
		let postData={
			// code:loginRes.code,
			//code值可能有所改变
			code:code,
			inviteCode:'',
		}
		resolve(gyzjModel.prototype.userLogin(postData));
	})
}
/*小程序的登录*/
Vue.prototype.Old_main_login=function(){
	console.log('执行了登陆进来');
	// 1.wx获取登录用户code
	return new Promise((resolve,reject)=>{
		uni.login({
			provider: 'weixin',
			success: function(loginRes) {
				console.log('微信登录',loginRes);
				let postData={
					// code:loginRes.code,
					//code值可能有所改变
					code:loginRes.authResult.access_token,
					inviteCode:'',
				}
				resolve(gyzjModel.prototype.userLogin(postData));
			},
		});
	})
}
/*全局验证登录*/
Vue.prototype.main_is_login=function(arr,url){
	if(arr.type==2){
		if(arr.token){
			/*默认手机号进来*/
			if (arr.token == '0739') {
				uni.showToast({
					title: '请您先登录',
				})
				uni.navigateTo({
					url: '../login/login-select/login-select',
				});
				
				/*自己手机号进来*/
			} else {
				uni.navigateTo({
					url: url,
				});
			}
		}
	}else if(arr.type==1){
		//入驻进来跳入驻页
		if(this.userInfo.type==1){
			uni.navigateTo({
				url: url,
			});
			/*否则跳自己页*/
		}else{
			uni.navigateTo({
				url: url,
			});
		}
	}
}
/*全局店铺登录*/
Vue.prototype.main_is_merchant=function(arr,url,userInfo){
	if(arr.type==2){
		if(arr.token){
			if (arr.token == '0739') {
				uni.showToast({
					title: '请您先登录',
				})
				uni.navigateTo({
					url: '../login/login-select/login-select',
				});
				uni.hideToast();
			} else {
				/*如果有手机号*/
				if (userInfo.is_merchant == 1) {
					uni.navigateTo({
						url: '../merchant/no-merchant/no-merchant',
					})
				} else {
					uni.navigateTo({
						url: '../merchant/my-merchant/my-merchant',
						// url: '../merchant/no-merchant/no-merchant',
					})
				}
			}
		}
	}else if(arr.type==1){
		if(this.userInfo.type==1){
			uni.navigateTo({
				url: url,
			});
		}else{
			uni.navigateTo({
				url:url,
			});
		}
	}
}
/*全局得到用户信息*/
Vue.prototype.main_getUserInfo=function(token,type){
	return new Promise((resolve,reject)=>{
		resolve(gyzjModel.prototype.getUserInfo(token,type));
	})
}
Vue.prototype.main_getGlobal=function(){
	let globalData = App.globalData;
	var type, token, arr = new Array();
	console.log('main_getGlobal进来',globalData);
	/*优先globalData，如果没有再缓存*/
	if (globalData.mobile||uni.getStorageSync('mobile')) {
		arr['type'] = 2;
		arr['token'] = globalData.mobile;
		console.log('main_getGlobal的arr为mobile', arr);
		/*这里token有过期时间*/
	} else if (globalData.token) {
		arr['type'] = 1;
		arr['token'] = globalData.token;
		console.log('main_getGlobal的arr为token', arr);
	}
	return arr;
}			 
			 
Vue.component('page-head', pageHead)
Vue.component('page-foot', pageFoot)
Vue.component('uLink', uLink)
// Vue.component('uni-rate',uniRate)
App.mpType = 'app'

const app = new Vue({
	store,
	...App
})
app.$mount()
